<?php

namespace Kordar\Excel;

use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use yii\data\ActiveDataProvider;

class OutputExcelEvent extends \yii\base\Event implements OutputExcelInterface
{
    /**
     * @var null | ActiveDataProvider | array
     */
    public $dataProvider = null;

    /**
     * @var int
     */
    public $row = 1;

    /**
     * @var array | null
     */
    public $columns = [];

    /**
     * @var int
     */
    public $maxRow = 2000;

    /**
     * @var null
     */
    public $filename = null;

    /**
     * @var null
     */
    protected $viewListClass = null;

    public function render(Worksheet $sheet)
    {
        $this->dataProvider->setPagination(['pageSize' => $this->maxRow]);
        // TODO: Implement render() method.
        $object = \Yii::createObject([
            'class' => ExcelGridSheet::className(),
            'row' => $this->row,
            'dataProvider' => $this->dataProvider,
            'columns' => $this->columns,
            'sheet' => $sheet
        ]);
        $object->run();
        return $sheet;
    }

    public function filename($filename)
    {
        // TODO: Implement filename() method.
        return $this->filename ?: $filename;
    }
}